<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>实物资产管理</title>
    <script src="../../../../../res/js/jquery-3.4.1.min.js" charset="utf-8"></script>
    <script src="../../../../../res/js/login_common.js" charset="utf-8"></script>
    <script src="../../../../../res/layui/layui.js" charset="utf-8"></script>
    <script src="../../../../../res/js/Tools.js" charset="utf-8"></script>
    <script src="../../../../../res/js/error.js" charset="utf-8"></script>
    <link rel="shortcut icon" type="image/x-icon" href="../../../../../res/images/favicon.ico">
    <link rel="stylesheet" href="../../../../../res/layui/css/layui.css" media="all">
</head>
<body style="padding: 10px">
<form class="layui-form layui-form-pane" action="">
    <input type="hidden" id="pkAssetId" name="pkAssetId"/>
    <div class="layui-form-item">
        <label class="layui-form-label">资产条码</label>
        <div class="layui-input-block">
            <input autocomplete="off" class="layui-input" name="assetBarcode" placeholder="必填"
                   id="assetBarcode" type="text" lay-verify="required"/><span id="msg"></span>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">资产名</label>
        <div class="layui-input-block">
            <input autocomplete="off" class="layui-input" id="assetName" name="assetName"
                   type="text" lay-verify="required" placeholder="必填"/>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-inline">
        <label class="layui-form-label">资产的位置</label>
        <div class="layui-input-inline">
            <select  name="assetCategory" id="assetCategory" lay-filter="assetCategory">
                <option value="0" selected="">部门</option>
                <option value="1" >仓库</option>
            </select>
        </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">位置</label>
            <div class="layui-input-block" id="CategoryId1" >
                <select name="assetCategoryId" id="assetCategoryId">
                </select>
            </div>
            <div class="layui-input-block" id="CategoryId2" style="display: none">
                <select  name="assetCategoryId" id="assetCategoryId2">
                </select>
            </div>
        </div>


    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">资产金额</label>
        <div class="layui-input-block">
            <input type="text" name="assetPrice" id="assetPrice"
                   lay-verify="required|number" placeholder="请输入" autocomplete="off" class="layui-input" placeholder="必填">
        </div>
    </div>

    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">资产状态</label>
        <div class="layui-input-block">
            <select name="assetState" lay-filter="aihao" id="assetState">
                <option value="0">闲置</option>
                <option value="1" selected="">使用中</option>
                <option value="2">报废</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block" id="flows">
            <button type="submit" class="layui-btn site-demo-active" lay-submit="" lay-filter="demo1">立即提交</button>
            <input type="button" id="demo3" class="layui-btn layui-btn-primary" value="取消">
        </div>
    </div>
</form>

<script>
    var form = null;
    let pkAssetId = getQueryString("pkAssetId");
    // 修改赋值
    layui.use(['form', 'layer'], function () {
             layer = layui.layer
            , form = layui.form;
        //自定义
        form.on('select(assetCategory)', function(data){
            //判断是否0为部门或者1仓库
            if(data.value == 0){
                $("#CategoryId2").hide();
                $("#CategoryId1").show();
                form.render('select');
            }else{
                $("#CategoryId2").show();
                $("#CategoryId1").hide();
                form.render('select');//select是固定写法 不是选择器
            }
        });
         loadType();
         loadCategoryId();
         if (pkAssetId){
              findById(pkAssetId);
          }
          form.render();


        //监听提交
        form.on('submit(demo1)', function (data) {
            if($("#assetCategory").val()==="0"){
                data.field.assetCategoryId=$("#assetCategoryId").val();
            }else{
                data.field.assetCategoryId=$("#assetCategoryId2").val();
            }
            console.log(data.field)
            debugger
            let res = postAjax(getRootPath() + "OaAssetAction/assetAddOrUpdate", data.field);
            if (res.code === 1) {
                layer.msg('操作成功3秒后关闭当前窗口', {icon: 1});
                setTimeout(function () {
                    formClose();
                }, 3000);
                setTimeout(function () {
                    parent.layui.table.reload('test');//刷新父页面
                    //window.parent.location.reload();//刷新父页面
                    const index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                    parent.layer.close(index);
                }, 3001);
                //window.location.reload();//刷新页面
            } else if (res.code === 500) {

            } else {
                layer.msg("新增失败");
            }
            return false;
        });

        $("#demo3").on("click", function () {
            //询问框
            layer.confirm('是否关闭该页面？', {
                btn: ['是的', '取消'] //按钮
            }, function () {
                formClose();
                parent.layui.table.reload('test');//刷新父页面
                //window.parent.location.reload();//刷新父页面
                const index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                parent.layer.close(index);
            });
        });

    });

        function findById(pkAssetId) {
            let url = getRootPath() + "/OaAssetAction/assetFindById/" + pkAssetId;
            let data = getAjaxById(url);
            console.log(data);
            $("#pkAssetId").val(data.pkAssetId);
            $("#assetBarcode").val(data.assetBarcode);
            $("#assetName").val(data.assetName);
            $("#assetCategory").val(data.assetCategory);
            if (data.assetCategory==="0"){
                $("#CategoryId1").show();
                $("#CategoryId2").hide();
                $("#assetCategoryId").val(data.assetCategoryId);
                form.render('select');
            }else{
                $("#CategoryId2").show();
                $("#CategoryId1").hide();
                console.log(data.assetCategoryId);
                $("#assetCategoryId2").val(data.assetCategoryId);
                form.render('select');//select是固定写法 不是选择器
            }
            $("#assetPrice").val(data.assetPrice);
            $("#assetState").val(data.assetState);
            layui.form.render("select");
        }
        /*哪个部门*/
    function loadType(){
        let url=getRootPath()+"OaDepartmentAction/departmentfindByAll";
        let data=getAjax(url,null);
        $.each(data.data,function(index,item){
            $("#assetCategoryId").append("<option value='"+item.pkDepartmentId+"'>"+item.departmentName+"</option>");
        });
    }

    /*哪个仓库*/
    function loadCategoryId(){
        const nation = getAjaxById(getRootPath() + "OaDataDictionaryAction/findKeyword/WAREHOUSE");
        $.each(nation, function (index, item) {
            $("#assetCategoryId2").append("<option value='"+item.pkDataDictionaryId+"'>"+item.dataDictionaryName+"</option>");
        });
    }
    $(function(){
        //给文本框绑定失去焦点的事件
        $("#assetBarcode").blur(function(){
            var message = '';
            if (pkAssetId==null){
                const res=getAjax(getRootPath() + "OaAssetAction/selectCode",{assetBarcode:this.value});
                console.log(res);
                if(res.code){
                    message ="资产条码已存在，请重新输入";
                    $("#assetBarcode").val("");
                    $("#msg").css("color","red").html(message);
                    return false;
                }else{
                    message='';
                }
            }else{
                console.log(this.value);
                const res=getAjax(getRootPath() + "OaAssetAction/selectCode",{assetBarcode:this.value,pkAssetId:pkAssetId});
                if(res.code){
                    message='资产条码已存在，请重新输入';
                    $("#assetBarcode").val("");
                    $("#msg").css("color","red").html(message);
                    return false;
                }else{
                    message='';
                }
            }
            //需要注意 需要将返回信息写在ajax方法外
            if (message !== '')
                return message;
        })
    });
</script>
</body>
</html>